/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ge.info.pages;

import ge.info.domain.DbExecutor;
import ge.info.logging.Session;
import ge.info.sip.SessionMap;
import javax.servlet.http.HttpServletRequest;
import org.apache.tapestry.BaseComponent;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.InitialValue;
import org.apache.tapestry.annotations.InjectState;
import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEndRenderListener;
import org.apache.tapestry.event.PageEvent;

/**
 *-
 * @author koka
 */
public abstract class ChargeButton extends BaseComponent implements PageEndRenderListener, PageBeginRenderListener
{

    @InjectObject("spring:CurrentSessions")
    public abstract SessionMap getCurrentSessions();

    @InjectObject("service:tapestry.globals.HttpServletRequest")
    public abstract HttpServletRequest getRequest();

    @InjectObject("spring:dbExecutor")
    public abstract DbExecutor getDbExecutor();

    public abstract int getServiceID();

    public abstract void setServiceID(int id);

    @InitialValue("false")
    public abstract boolean isChargeClicked();

    public abstract void setChargeClicked(boolean b);

    @Persist
    public abstract Session getSession();
    public abstract void setSession(Session session);

    public ChargeButton()
    {
        super();
    }

    public void submit(IRequestCycle cycle)
    {
        setChargeClicked(true);
        if (!getSession().isWebSession())
        {
            if (getServiceID()==3)
            {
                OrgResultPage orp = (OrgResultPage)getPage();
               
                getDbExecutor().insorgcharge(getServiceID(), getSession(),orp.getOid());
                 getSession().setChcount(getSession().getChcount() + 1);
            }
             if (getServiceID()== -3)
            {
                

                    OrgResultInfo ori =(OrgResultInfo) getPage();
                  
                    getDbExecutor().insorgcharge(getServiceID(), getSession(),ori.getOid());
                    getSession().setChcount(getSession().getChcount() + 1);
                   
                }
            if (getServiceID()!=3 && getServiceID()!= -3)
            {
            getDbExecutor().inscharge(getServiceID(), getSession());
            getSession().setChcount(getSession().getChcount() + 1);
            }
            }

    }

    public void pageBeginRender(PageEvent event)
    {
        if (!event.getRequestCycle().isRewinding())
        {
            setSession(getCurrentSessions().get(getRequest().getRemoteAddr()));
        }
    }

    public void pageEndRender(PageEvent event)
    {
        if (!event.getRequestCycle().isRewinding())
        {
            setChargeClicked(false);
        }
    }
}
